package com.tbit.uqbike.webmanager.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BrandDiscountConfig;
import com.tbit.uqbike.object.pojo.dto.BrandDiscountConfigDTO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BrandDiscountConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 特殊人群类型配置
 *
 * @Author:BUGTian
 * @DATE: 2022/9/21
 */
@RestController
@RequestMapping("/brandDiscountConfig")
public class BrandDiscountConfigController {

    @Autowired
    private BrandDiscountConfigService brandDiscountConfigService;


    /**
     * 查询
     *
     * @param accountId 品牌id
     * @return
     */
    @GetMapping("/query")
    public Result<List<BrandDiscountConfig>> query(Integer accountId) {
        List<BrandDiscountConfig> list = brandDiscountConfigService.list(new QueryWrapper<BrandDiscountConfig>().eq("accountId", accountId));
        return Result.success(list);
    }

    /**
     * 获取所有
     *
     * @return
     */
    @GetMapping("/getAll")
    public Result<List<BrandDiscountConfig>> getAll() {
        return Result.success(brandDiscountConfigService.getAll());
    }

    /**
     * 新增
     *
     * @param brandDiscountConfigDTO
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody BrandDiscountConfigDTO brandDiscountConfigDTO) {
        if (StrUtil.isNotEmpty(brandDiscountConfigDTO.getName()) && brandDiscountConfigDTO.getName().length() > 100){
            throw new BaseException("认证类型名称过长");
        }else if (StrUtil.isNotEmpty(brandDiscountConfigDTO.getRemark()) && brandDiscountConfigDTO.getRemark().length() > 255){
            throw new BaseException("认证类型备注过长");
        }
        return brandDiscountConfigService.insert(brandDiscountConfigDTO);
    }

    /**
     * 删除
     *
     * @param configId 主键
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(Integer configId) {
        return brandDiscountConfigService.delete(configId);
    }

    /**
     * 更新
     *
     * @param brandDiscountConfig
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody BrandDiscountConfig brandDiscountConfig) {
        if (brandDiscountConfigService.count(new QueryWrapper<BrandDiscountConfig>().eq("name", brandDiscountConfig.getName()).eq("accountId", brandDiscountConfig.getAccountId()).ne("configId", brandDiscountConfig.getConfigId())) > 0) {
            return Result.error("类型名称重复");
        }
        checkLength(brandDiscountConfig);
        brandDiscountConfigService.updateById(brandDiscountConfig);
        return Result.success();
    }

    void checkLength(BrandDiscountConfig brandDiscountConfig){
        if (StrUtil.isNotEmpty(brandDiscountConfig.getName()) && brandDiscountConfig.getName().length() > 100){
            throw new BaseException("认证类型名称过长");
        }else if (StrUtil.isNotEmpty(brandDiscountConfig.getRemark()) && brandDiscountConfig.getRemark().length() > 255){
            throw new BaseException("认证类型备注过长");
        }
    }
}
